package com.czl.elm.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czl.elm.dao.UsersMapper;
import com.czl.elm.dao.impl.UsersMapperImpl;
import com.czl.elm.domain.Users;
import com.czl.elm.domain.UsersLoginParam;
import com.czl.elm.service.IUsersService;
import com.czl.elm.util.ActionResult;
import com.czl.elm.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zuoleiChen
 * @since 2021-05-15
 */
@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements IUsersService {

    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private UsersMapperImpl usersMapperimpl;



    @Value("${file.upload.url}")
    private String uploadFilePath;

    /**
     * 获取用户的所有属性
     * @return
     */
    @Override
    public ActionResult getUsersProperty() {
        String phoneName=((UsersLoginParam) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getPhoneName();
        Users user=usersMapper.selectUserByName(phoneName);
        if(user==null)return ActionResult.error("获取失败");
        user.setPassword(null);

        return ActionResult.success("成功获取信息",user);

    }

    /**
     * 修改用户生日
     * @param birthday
     * @return
     */
    @Override
    public ActionResult setUsersBirthday(String birthday) {
        //SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //System.out.println(birthday);
        //String data=simpleDateFormat.format(birthday);
        String phoneName= ((UsersLoginParam) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getPhoneName();
        if(usersMapper.setUsersBirthday(phoneName,birthday)>0){
            return ActionResult.success("生日修改成功");
        }else{
            return ActionResult.error("修改失败");
        }
    }

    /**
     * 修改用户的头像
     * @param profile
     * @return
     */
    @Override
    public ActionResult setUsersProfile(MultipartFile profile) {
        String phoneName= ((UsersLoginParam) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getPhoneName();
        String filetype=profile.getOriginalFilename().split("\\.")[1];
        //System.out.println(filetype);
        if(!FileUtil.imgCheck(filetype)){
            return ActionResult.error("文件类型不正确");
        }

        String path="\\profile\\"+phoneName+"_profile."+filetype;
        File dest = new File(uploadFilePath + path);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            profile.transferTo(dest);
        } catch (Exception e) {
            log.error("{}",e);
            return ActionResult.error("上传文件失败");
        }
        if(usersMapper.setUsersProfile(path,phoneName)>0){
            JSONObject obj=new JSONObject();
            obj.put("url",path);
            return ActionResult.success("上传成功",obj);
        }else{
            return ActionResult.error("上传失败");
        }
    }

    /**
     * 修改用户的密码
     * @param checkCode
     * @param newpass
     * @return
     */
    @Override
    public ActionResult setUsersPassword(String checkCode,String newpass) {
//        String checkCode=password.get("checkCode");
//        String newpass=password.get("newpass");
        String phoneName= ((UsersLoginParam) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getPhoneName();

        //Users user=usersMapper.selectUserByName(phoneName);
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String code="1111";
        if(!code.equals(checkCode)){
            return ActionResult.error("手机验证码不正确");
        }
        if(usersMapper.setUsersPassword(encoder.encode(newpass),phoneName)>0){
            return ActionResult.success("修改密码成功");
        }else{
            return ActionResult.error("修改密码失败");
        }

    }

    /**
     * 修改用户的昵称
     * @param userName
     * @return
     */
    @Override
    public ActionResult setUsersUserName(String userName) {
        String phoneName= ((UsersLoginParam) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getPhoneName();
        if(usersMapper.setUsersUserName(phoneName,userName)>0){
            return ActionResult.success("修改昵称成功");
        }else{
            return ActionResult.error("修改昵称失败");
        }

    }

    /**
     * 修改用户的简介
     * @param info
     * @return
     */
    @Override
    public ActionResult setUsersInfo(String info) {
        String phoneName= ((UsersLoginParam) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getPhoneName();
        if(usersMapper.setUsersInfo(phoneName,info)>0){
            return ActionResult.success("修改简介成功");
        }else{
            return ActionResult.error("修改简介失败");
        }
    }

//    public Address
    /**
     * 新增地址
     * @param msg
     * @return
     */
    @Override
    public ActionResult addUsersAddress(Map<String, Object> msg) {

        msg=(Map<String, Object>)msg.get("msg");
        String phoneName= ((UsersLoginParam) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getPhoneName();
        usersMapperimpl.addUsersAddress(phoneName,msg,null);
        return ActionResult.success("添加成功");

    }

    @Override
    public ActionResult getUsersAddress() {
        String phoneName= ((UsersLoginParam) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getPhoneName();
        Map<String,Object> res=usersMapperimpl.getUsersAddress(phoneName);
        //if(res==null)return ActionResult.error("获取失败");
        return ActionResult.success("获取成功",res);
    }


    @Override
    public ActionResult delUsersAddress(String addressId) {
        String phoneName= ((UsersLoginParam) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getPhoneName();
        boolean res=usersMapperimpl.delUsersAddress(phoneName,addressId);
        if(res)return ActionResult.success("删除成功");
        else return ActionResult.error("找不到此地址");
    }

    @Override
    public ActionResult setDefaultUsersAddress(String addressId) {
        String phoneName= ((UsersLoginParam) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getPhoneName();
        usersMapperimpl.setDefaultUsersAddress(phoneName,addressId);
        return ActionResult.success("默认地址修改成功");
    }


    @Override
    public ActionResult setUsersAddress(String addressId,Map<String,Object> msg) {
        msg=(Map<String, Object>)msg.get("msg");
        String phoneName= ((UsersLoginParam) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getPhoneName();
        usersMapperimpl.setUsersAddress(phoneName,addressId,msg);
        return ActionResult.success("地址修改成功");
    }

    @Override
    public ActionResult setShopCart(String shopId, Map<String, Object> msg) {
        String phoneName= ((UsersLoginParam) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getPhoneName();
        usersMapperimpl.setShopCart(phoneName,shopId,msg);
        return ActionResult.success("设置购物车成功");
    }

    @Override
    public ActionResult getShopCart(String shopId) {
        String phoneName= ((UsersLoginParam) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getPhoneName();
        Map<String, Object> msg=usersMapperimpl.getShopCart(phoneName,shopId);
        if(msg!=null)msg.remove("_id");
        return ActionResult.success("获取购物车成功",msg);
    }
}
